package weatherclient;

import java.awt.Dialog;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Toolkit;
import java.io.IOException;
import javax.swing.JButton;

/**
 * Adatletöltést megjelenítő ablak.
 *
 * @author imruf84
 */
public final class DownloadDataFrame extends javax.swing.JFrame {

    /**
     * Letöltöt adatok számlálója.
     */
    private int counter = 0;

    /**
     * Creates new form DownloadDataFrame
     *
     * @throws java.awt.FontFormatException kivétel
     * @throws java.io.IOException kivétel
     */
    public DownloadDataFrame() throws FontFormatException, IOException {
        initComponents();

        setLocationRelativeTo(null);

        setResizable(false);
        setEnabled(false);
        setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);

        messagesTextArea.setEditable(false);
        Font font = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream("/weatherclient/fonts/cour.ttf")).deriveFont(Font.PLAIN, 14);
        messagesTextArea.setFont(font);
    }

    @Override
    public void setEnabled(boolean b) {

        setDefaultCloseOperation(b ? DISPOSE_ON_CLOSE : DO_NOTHING_ON_CLOSE);

        if (!b) {
            return;
        }

        getContentPane().remove(downloadingProgressBar);
        JButton closeButton = new JButton("Bezár");
        closeButton.addActionListener(e -> {
            dispose();
        });
        getContentPane().add(closeButton, java.awt.BorderLayout.PAGE_END);
        pack();
        closeButton.requestFocus();
        if (b) {
            WeatherClientFrame.refreshData();
        }
    }

    /**
     * Szöveg írása a szövegdobozba.
     *
     * @param s szöveg
     */
    public void write(final String s) {
        messagesTextArea.setText((WeatherData.isDataStringValid(s.replaceAll("\n", "")) ? ((++counter) + ".") : "HIBA") + "\t" + s + messagesTextArea.getText());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        messagesTextArea = new javax.swing.JTextArea();
        downloadingProgressBar = new javax.swing.JProgressBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Adatok letöltése...");
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("weatherclient/icons/MainFrameIcon.png")));
        setMinimumSize(new java.awt.Dimension(400, 500));

        messagesTextArea.setColumns(20);
        messagesTextArea.setRows(5);
        jScrollPane1.setViewportView(messagesTextArea);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        downloadingProgressBar.setFocusable(false);
        downloadingProgressBar.setIndeterminate(true);
        getContentPane().add(downloadingProgressBar, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JProgressBar downloadingProgressBar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea messagesTextArea;
    // End of variables declaration//GEN-END:variables
}
